#!/bin/bash

# creator - zhenyujian
# date    - 2017-08-14 12:00:00
# desc    -
# 配置: 
#     keyadmin_rsa_path 私匙所在路径
#     host_array 服务器列表 (可选)
#     host_config_path 服务器列表配置文件 (可选)
#
#     注: host_array 中的host_username可以配置多个,以逗号分隔,不指定user时默认会使用第一个
#     注: 推荐配置文件的形式 即程序优先读取host_config_path 若文件不存在 则读取脚本内置的host_array
#         配置文件内容如 host_array数组元素 每行一个 (无双引号)
#           
#
# 参数:
#     show - 传递此参数时 则命令不会被执行，而是被打印显示
#     user - 传递此参数时 会提示选择用哪个用户登录服务器，可选的用户列表为 host_array中的host_username
#     scp  - 传递此参数时 会执行scp操作 (不传时默认执行ssh操作)
#
#
# 其它:
#     配置命令 
#     如: 编辑 /etc/bashrc 
#	      加入 alias mzssh='/Users/zhenyujian/.ssh/mz_ssh.sh'
#		  source ~/.bashrc
#
#


#----------------- property ------------------
#property - rsa path
keyadmin_rsa_path='/Users/tzp/.ssh/id_rsa_keyadmin'


#property - host list
host_array=("tianzhipeng ssh7.cn.miaozhen.com root h127.mzhen.cn 广协IP库测试home/data/work/tomcat|端口8110"
			"tianzhipeng ssh7.cn.miaozhen.com root 2581.mzhen.cn 广协IP库home/data/ipb"
			"tianzhipeng ssh7.cn.miaozhen.com root 2704.mzhen.cn dic-manager-旧版"
			"tianzhipeng ssh6.cn.miaozhen.com root,jenkins 3086.mzhen.cn 内部系统—测试环境"
			"tianzhipeng ssh7.cn.miaozhen.com root 50.mzhen.cn mm线上环境")

transfer_username_index=0 #跳板机登录名
transfer_index=1          #跳板机地址
host_username_index=2     #目标机登录用户
host_index=3              #目标机地址
desc_index=4              #描述


#property - host list - configFile
host_config_path='/Users/tzp/.ssh/mz_ssh_host'
function initProperties(){
	#host_config
	if [ -e "$host_config_path" ]; then
		host_array=()
		local index=0
		while read line
		do
			local lineTirm=`echo $line | sed -e "s/^[ \s]\{1,\}//g" | sed -e "s/[ \s]\{1,\}$//g"`
			echo $lineTirm
			if [ "$lineTirm" != "" ]; then
				host_array[index++]="$lineTirm"
			fi
		done <$host_config_path
	fi
}
initProperties


#----------------- param ------------------
#shell param
shell_param_key_show="show"
shell_param_key_user="user"
shell_param_key_scp="scp"

shell_param_input_show=false
shell_param_input_user=false
shell_param_input_scp=false

#read input param
for arg in $*  
do  
    if [ $arg = $shell_param_key_show ] ;then
		shell_param_input_show=true
	elif [ $arg = $shell_param_key_user ] ;then
		shell_param_input_user=true
	elif [ $arg = $shell_param_key_scp ] ;then
		shell_param_input_scp=true
	fi
done  


#----------------- function ------------------
# common function
function colorEcho(){
	echo -e "\033[34m$* \033[0m \c"
}

function echoTitle(){
	local title=$1
	echo "-----------------------------------------------"
    echo $title
	echo "-----------------------------------------------"
}

function echoTitleN(){
	echo -e "\n"
	local title=$1
	echoTitle $title
}

function echoFooter(){
	echo "-----------------------------------------------"
}

function splitStr(){  
    local str=$1;
  
    OLD_IFS="$IFS"
    IFS=","
    array=($str) 
    IFS="$OLD_IFS"
	
    echo ${array[*]}
}  



#bussiness function
function showHostList(){
	local host_size=${#host_array[*]}
	
	echoTitle "Host"
	for((i=0;i<$host_size;i++));
	do
		data=(${host_array[i]})
		echo "$((i))	${data[$host_index]}	${data[$desc_index]}"
	done  
	echoFooter
}



function inputNumber(){
	local size=$1
	local input_number=-1
	local prompt="please input the number:"

	while true 
	do
		colorEcho $prompt
		read input

		if  [ ! -n "$input" ] ;then
			prompt="please input the number:"
			continue;
		else 
			for((i=0;i<$size;i++));
			do
				if  [ $i = "$input" ] ;then
					input_number=$i;
				fi
			done

			if [ $input_number != -1 ] ;then
				break;
			else
				prompt="wrong number, please input the number:"
			fi
		fi  
	done

	return $input_number
}



function selectUser(){
	local host_num=$1
	local selected_host=(${host_array[$host_num]})
	local selected_host_username=${selected_host[$host_username_index]}
	local user_array=(`splitStr $selected_host_username`)
	local user_size=${#user_array[*]}
	
	#if  [ 1 -eq $user_size ] || [ $shell_param_input_user = false ] ;then
	if  [ $shell_param_input_user = false ] ;then
		return 0;
	else
		echoTitleN "User"
		for((i=0;i<$user_size;i++));
		do
			echo "$((i))	${user_array[i]}"
		done  
		echoFooter

		inputNumber ${user_size}
		return $?
	fi
}



#check ssh-agent
function checkSSH(){
	local added=$( ssh-add -l|grep ${keyadmin_rsa_path} -c )
	if  [ 0 = "$added" ] ;then
		ssh-add ${keyadmin_rsa_path}
	fi
}



#do ssh/scp
function doSSH(){
	local host_num=$1
	local user_num=$2

	#host
	local selected_host=(${host_array[$host_num]})

	#user
	local selected_host_username=(${selected_host[$host_username_index]})
	local user_array=(`splitStr $selected_host_username`)
	local selected_user=${user_array[$user_num]}

	#ssh command
	local command="ssh -o ProxyCommand='ssh ${selected_host[$transfer_username_index]}@${selected_host[$transfer_index]} -W %h:%p' ${selected_user}@${selected_host[$host_index]}"

	#scp 
	if [ $shell_param_input_scp = true ] ;then
		local scp_type_array=("upload" "download")
		local scp_type_size=${#scp_type_array[*]}
		
		echoTitleN "SCP Type"
		for((i=0;i<$scp_type_size;i++));
		do
			echo "$((i))	${scp_type_array[i]}"
		done  
		echoFooter

		inputNumber ${scp_type_size}
		local scp_type=$?
		local local_path
		local remote_path
		
		#input local path
		while true 
		do
			colorEcho "local path:"
			read input

			if  [ ! -n "$input" ] ;then
				continue;
			else 
				local_path=$input
				break
			fi  
		done

		#input remote path
		while true 
		do
			colorEcho "remote path:"
			read input

			if  [ ! -n "$input" ] ;then
				continue;
			else 
				remote_path=$input
				break
			fi  
		done

		#scp command
		command="scp -o ProxyCommand='ssh ${selected_host[$transfer_username_index]}@${selected_host[$transfer_index]} -W %h:%p'"

		if [ $scp_type = 0 ] ;then 
			#upload
			command="${command} ${local_path} ${selected_user}@${selected_host[$host_index]}:${remote_path}"
		elif [ $scp_type = 1 ] ;then
			#download
			command="${command} ${selected_user}@${selected_host[$host_index]}:${remote_path} ${local_path}"
		fi
		
	fi

	if [ $shell_param_input_show = true ] ;then
		echo ${command}
	else
		eval ${command}
	fi
}



#----------------- main ------------------
showHostList
inputNumber ${#host_array[*]}
host_num=$?
selectUser $host_num
user_num=$?
checkSSH
doSSH $host_num $user_num
